﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService


<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="关于我们"/>

@if(isReady)
{
    <MDCard Class="mb-4">
        <MTabs @bind-Value="TabIndex" IconsAndText GrowCenterActive>
         @foreach (var item in _dataCacheService.DocumentsCatche)
            {
                <MTab Value="@_dataCacheService.DocumentsCatche.IndexOf(item)">
                    @item.Title<MIcon>@item.Icon</MIcon>
                </MTab>
            }
        </MTabs>
    </MDCard>
<MTabsItems @bind-Value="TabIndex">
    @foreach (var item in _dataCacheService.DocumentsCatche)
    {
        <MTabItem Value="@_dataCacheService.DocumentsCatche.IndexOf(item)">
            <CnGalWebSite.Shared.MasaComponent.App.Home.DocumentCardGroup Model="item.Children" Id="Index" />
        </MTabItem>
    }
</MTabsItems>
}
else
{
    <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill/>
}



@code {
    [Parameter]
    public long Index { get; set; } = -1;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    public StringNumber TabIndex { get; set; } = 0;

    bool isReady;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            if (_dataCacheService.DocumentsCatche.Count == 0)
            {
                _dataCacheService.DocumentsCatche = await _httpService.GetAsync<List<DocumentViewModel>>("api/home/GetDocuments");
            }

            foreach (var item in _dataCacheService.DocumentsCatche)
            {
                if (item.Children.Any(s => s.Id == Index))
                {
                    TabIndex = _dataCacheService.DocumentsCatche.IndexOf(item);
                    break;
                }
            }
            isReady = true;
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取文档列表失败");
        }
    }
}
